01 / 08

What is the Execution Context?

  1. 1

    When the JavaScript engine scans a script file, it makes an environment called the Execution Context that handles the entire transformation and execution of the code.

  2. 2

    During the context runtime, the parser parses the source code and allocates memory for the variables and functions. The source code is generated and executed.

Difficulty: 5/10
Topics: scope chain, this binding, call stack

Scenario Questions

0-2 years experience
  1. 1

    If you write a simple function that logs a variable defined outside it, can you describe what execution context is created when the function runs and how the variable is resolved?

  2. 2

    What will be the value of this inside a regular function passed to setTimeout, and why does the execution context matter?

2-5 years experience
  1. 1

    You have a function that returns another function, and the inner function accesses a variable from the outer scope. During debugging you see unexpected values. Explain how the execution context and closure interact here.

  2. 2

    A teammate reports that a function works in the browser but throws a ReferenceError when run in Node. Walk me through how the different execution contexts could cause this.

5-8 years experience
  1. 1

    Our front‑end app suffers from memory leaks after navigating between pages. How would you investigate whether lingering execution contexts are the cause, and what strategies would you use to mitigate them?

  2. 2

    We need to refactor a large codebase to use ES modules instead of IIFEs. Discuss how the change in execution context affects this binding and the global scope, and what edge cases you’d watch for.

8+ years experience
  1. 1

    We are planning a migration from a monolithic JavaScript service to a micro‑frontend architecture. How would the shift in execution contexts across separate bundles influence shared state, and what patterns would you adopt to keep contexts isolated yet allow controlled communication?

  2. 2

    When designing a server‑side rendering pipeline that runs user‑provided scripts in a sandbox, how do you manage execution contexts to ensure security and performance, and what trade‑offs exist between using separate VMs versus a single shared context?

Follow-up Questions

  • What happens to the execution context when an async callback is queued and later executed?
  • How does strict mode change the this binding inside a function's execution context?
  • If an error is thrown inside a nested function, what does the call stack reveal about the active execution contexts?